import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.resps.Tuple;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RedisDemoZSet {
    public static void  test1(Jedis jedis) {
        System.out.println("zadd zrange zrangewithscores");
        jedis.flushAll();

        jedis.zadd("key", 10, "zhangsan");
        Map<String, Double> map = new HashMap<>();
        map.put("lisi", 20.0);
        map.put("wangwu", 30.0);
        jedis.zadd("key", map);

        List<String> key = jedis.zrange("key", 0, -1);
        System.out.println(key);

        List<Tuple> res = jedis.zrangeWithScores("key", 0, -1);
        Tuple tuple = res.get(1);
        System.out.println(tuple.getElement());
        System.out.println(tuple.getScore());
        System.out.println(res);
    }

    public static void  test2(Jedis jedis) {
        System.out.println("zcard");
        jedis.flushAll();

        jedis.zadd("key", 10, "zhangsan");
        Map<String, Double> map = new HashMap<>();
        map.put("lisi", 20.0);
        map.put("wangwu", 30.0);
        jedis.zadd("key", map);

        long len = jedis.zcard("key");
        System.out.println(len);
    }

    public static void  test3(Jedis jedis) {
        System.out.println("zcard");
        jedis.flushAll();

        jedis.zadd("key", 10, "zhangsan");
        Map<String, Double> map = new HashMap<>();
        map.put("lisi", 20.0);
        map.put("wangwu", 30.0);
        jedis.zadd("key", map);

        System.out.println(jedis.zrangeWithScores("key", 0, -1));

        long i = jedis.zrem("key", "zhangsan", "lisi");
        System.out.println(i);
        List<Tuple> key = jedis.zrangeWithScores("key", 0, -1);
        System.out.println(key);

        Double score = jedis.zscore("key", "wangwu1");
        System.out.println(score);
    }

    public static void  test4(Jedis jedis) {
        System.out.println("zcard");
        jedis.flushAll();

        jedis.zadd("key", 10, "zhangsan");
        Map<String, Double> map = new HashMap<>();
        map.put("lisi", 20.0);
        map.put("wangwu", 30.0);
        jedis.zadd("key", map);

        Long rank = jedis.zrank("key", "lisi");
        System.out.println(rank);
    }


    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try (Jedis jedis = jedisPool.getResource()){
            test4(jedis);
        }
    }
}
